﻿using RoadQualityEvalAPI.Models;
using RoadQualityEvaluatorData.Domain.Entities;
using RoadQualityEvaluatorData.DomainServices.Generic;
using RoadQualityEvaluatorData.DomainServices.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace RoadQualityEvalAPI.Controllers
{
    public class TripStatsController : ApiController
    {

        private TripStatsRepository _tripStatsRepository;

        private UserRepository _userRepository;

        [HttpGet]
        public List<TripStatsModel> GetAllTripsStatistics()
        {
            var trips = new List<TripStatsModel>();

            _tripStatsRepository = new TripStatsRepository();

            if (_tripStatsRepository.GetAll().Count > 0)
            {
                foreach (var trip in _tripStatsRepository.GetAll())
                {
                    trips.Add(new TripStatsModel()
                    {
                        CreatedAt = trip.CreatedAt,
                        Info = trip.TripInfo,
                        TripAverageRoughness = trip.TripAverageRoughness,
                        TripAverageSpeed = trip.TripAverageSpeed,
                        TripDistance = trip.TripDistance,
                        TripNoOfSpeedbumps = trip.TripNoOfSpeedbumps,
                        TripNoOfPotholes = trip.TripNoOfPotholes,
                        TripDurationMillis = trip.TripDurationMillis
                    });
                }
            }

            return trips;
        }


        [HttpGet]
        public List<TripStatsModel> GetAllTripsStatisticsFromUser(string username)
        {
            var trips = new List<TripStatsModel>();

            UnitOfWork uo = new UnitOfWork();
            _tripStatsRepository = new TripStatsRepository(uo);
            _userRepository = new UserRepository(uo);

            var userWhoCollected = _userRepository.GetUserByUsername(username);

            if (_tripStatsRepository.GetAll().Where(ts => ts.UserWhoCollected.Id == userWhoCollected.Id).Count() > 0)
            {
                foreach (var trip in _tripStatsRepository.GetAll().Where(ts => ts.UserWhoCollected.Id  == userWhoCollected.Id))
                {
                    trips.Add(new TripStatsModel()
                    {
                        CreatedAt = trip.CreatedAt,
                        Info = trip.TripInfo,
                        TripAverageRoughness = trip.TripAverageRoughness,
                        TripAverageSpeed = trip.TripAverageSpeed,
                        TripDistance = trip.TripDistance,
                        TripNoOfSpeedbumps = trip.TripNoOfSpeedbumps,
                        TripNoOfPotholes = trip.TripNoOfPotholes,
                        TripDurationMillis = trip.TripDurationMillis
                    });
                }
            }

            return trips;
        } 


        public TripStatsModel GetTripStatistics(int id)
        {
            _tripStatsRepository = new TripStatsRepository();

            var trip = _tripStatsRepository.GetById(id);

            if (trip == null)
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));


            return new TripStatsModel()
            {
                CreatedAt = trip.CreatedAt,
                Info = trip.TripInfo,
                TripAverageRoughness = trip.TripAverageRoughness,
                TripAverageSpeed = trip.TripAverageSpeed,
                TripDistance = trip.TripDistance,
                TripNoOfSpeedbumps = trip.TripNoOfSpeedbumps,
                TripNoOfPotholes = trip.TripNoOfPotholes,
                TripDurationMillis = trip.TripDurationMillis           
            };

        }

        

        [HttpPost]
        public void AddTripStatistics(TripStatsModel trip)
        {
            UnitOfWork uo = new UnitOfWork();

            _userRepository = new UserRepository(uo);
            _tripStatsRepository = new TripStatsRepository(uo);


            if (ModelState.IsValid)
            {
                var userWhoCollected = _userRepository.GetUserByUsername(trip.Username);

                _tripStatsRepository.Add(new TripStats()
                {
                    CreatedAt = trip.CreatedAt,
                    TripInfo = trip.Info,
                    TripAverageRoughness = trip.TripAverageRoughness,
                    TripAverageSpeed = trip.TripAverageSpeed,
                    TripDistance = trip.TripDistance,
                    TripNoOfSpeedbumps = trip.TripNoOfSpeedbumps,
                    TripNoOfPotholes = trip.TripNoOfPotholes,
                    TripDurationMillis = trip.TripDurationMillis,
                    UserWhoCollected = userWhoCollected
                });

            }
        }


        public void AddTripStatsList(List<TripStatsModel> tripList)
        {
            UnitOfWork uo = new UnitOfWork();

            _userRepository = new UserRepository(uo);
            _tripStatsRepository = new TripStatsRepository(uo);

            if (tripList != null && tripList.Count > 0)
            {
                foreach (var trip in tripList)
                {
                    var userWhoCollected = _userRepository.GetUserByUsername(trip.Username);

                    _tripStatsRepository.Add(new TripStats()
                    {
                        CreatedAt = trip.CreatedAt,
                        TripInfo = trip.Info,
                        TripAverageRoughness = trip.TripAverageRoughness,
                        TripAverageSpeed = trip.TripAverageSpeed,
                        TripDistance = trip.TripDistance,
                        TripNoOfSpeedbumps = trip.TripNoOfSpeedbumps,
                        TripNoOfPotholes = trip.TripNoOfPotholes,
                        TripDurationMillis = trip.TripDurationMillis,
                        UserWhoCollected = userWhoCollected
                    });
                }
            }
        }



    }
}
